package com.iams.manage.service.impl;


import com.iams.common.core.domain.entity.SysUser;
import com.iams.common.utils.DateUtils;
import com.iams.common.utils.SecurityUtils;
import com.iams.common.utils.uuid.UUID;
import com.iams.manage.domain.ArchiveDestroyApproval;
import com.iams.manage.mapper.ArchiveDestroyApprovalMapper;
import com.iams.manage.service.IArchiveDestroyApprovalService;
import com.iams.manage.validator.DestroyApprovalValidator;
import com.iams.system.service.ISysUserService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

/**
 * 档案销毁Service业务层处理
 * 
 * @author LiuTao
 * @date 2025-03-18
 */
@Service
public class ArchiveDestroyApprovalServiceImpl implements IArchiveDestroyApprovalService 
{


    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ArchiveDestroyApprovalMapper archiveDestroyApprovalMapper;

    @Autowired
    private DestroyApprovalValidator destroyApprovalValidator;

    /**
     * 查询档案销毁
     * 
     * @param id 档案销毁主键
     * @return 档案销毁
     */
    @Override
    public ArchiveDestroyApproval selectArchiveDestroyApprovalById(Long id)
    {
        return archiveDestroyApprovalMapper.selectArchiveDestroyApprovalById(id);
    }

    /**
     * 查询档案销毁列表
     * 
     * @param archiveDestroyApproval 档案销毁
     * @return 档案销毁
     */
    @Override
    public List<ArchiveDestroyApproval> selectArchiveDestroyApprovalList(ArchiveDestroyApproval archiveDestroyApproval)
    {
        return archiveDestroyApprovalMapper.selectArchiveDestroyApprovalList(archiveDestroyApproval);
    }

    /**
     * 新增档案销毁
     * 
     * @param archiveDestroyApproval 档案销毁
     * @return 结果
     */
    @Override
    public int insertArchiveDestroyApproval(ArchiveDestroyApproval archiveDestroyApproval)
    {
        archiveDestroyApproval.setDeptName(sysUserService.selectUserById(SecurityUtils.getLoginUser().getUserId()).getDept().getDeptName());


        destroyApprovalValidator.validate(archiveDestroyApproval);

        archiveDestroyApproval.setCreateTime(DateUtils.getNowDate());
        archiveDestroyApproval.setStatus("待审批"); // 设置初始状态为待审批

        // 毕竟用ID不太直观，需要添加姓名的信息
        SysUser sysUser = sysUserService.selectUserById(archiveDestroyApproval.getUserId());
        if(archiveDestroyApproval.getProcessName() == null)
        {
            archiveDestroyApproval.setProcessName("ArchiveDestroy");
        }


        String businessKey = UUID.randomUUID().toString();
        archiveDestroyApproval.setBusinessKey(businessKey);

        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey(archiveDestroyApproval.getProcessName())
                .withBusinessKey(businessKey)
                .withName(sysUser.getUserName() + "档案销毁申请")
                .build());

        archiveDestroyApproval.setInstanceId(processInstance.getId());
        archiveDestroyApproval.setInstanceName(processInstance.getName());

        return archiveDestroyApprovalMapper.insertArchiveDestroyApproval(archiveDestroyApproval);
    }

    /**
     * 修改档案销毁
     * 
     * @param archiveDestroyApproval 档案销毁
     * @return 结果
     */
    @Override
    public int updateArchiveDestroyApproval(ArchiveDestroyApproval archiveDestroyApproval)
    {
        return archiveDestroyApprovalMapper.updateArchiveDestroyApproval(archiveDestroyApproval);
    }

    /**
     * 批量删除档案销毁
     * 
     * @param ids 需要删除的档案销毁主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDestroyApprovalByIds(Long[] ids)
    {
        return archiveDestroyApprovalMapper.deleteArchiveDestroyApprovalByIds(ids);
    }

    /**
     * 删除档案销毁信息
     * 
     * @param id 档案销毁主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDestroyApprovalById(Long id)
    {
        return archiveDestroyApprovalMapper.deleteArchiveDestroyApprovalById(id);
    }

    @Override
    public ArchiveDestroyApproval selectArchiveDestroyApprovalByBusinessKey(String businessKey) {
        return archiveDestroyApprovalMapper.selectArchiveDestroyApprovalByBusinessKey(businessKey);
    }
}
